package com.wejoy.video.middle.station.admin.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.system.SysRoleOrder;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.enums.dict.EnumOrderRoleType;
import com.wejoy.video.middle.station.data.service.evs.IEvsHttpService;
import com.wejoy.video.middle.station.data.service.system.ISysRoleOrderService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.util.baiduevs.EvsApiHttpUtils;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.vo.system.SysRoleResourceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;

/**
 * 系统-工单权限关联表
 *
 * @author admin
 * @date 2024年03月31日 21:52:42
 */
@RestController
@RequestMapping("/system/sysRoleOrder")
public class SysRoleOrderController extends BaseController {

    @Autowired
    private ISysRoleOrderService sysRoleOrderService;
    @Autowired
    private IEvsHttpService evsHttpService;

    /**
     * 保存或更新
     *
     * @param sysRoleOrderReq sysRoleOrderReq
     * @return RespJSON
     */
    @PostMapping("/addDeptRole")
    @RoleCheck(name = "sys_user_data")
    public RespJSON addDeptRole(@RequestBody SysRoleOrder sysRoleOrderReq,@SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        WJAssert.notNull(sysRoleOrderReq,"参数错误");
        WJAssert.notNull(sysRoleOrderReq.getRoleType(),"参数错误");
        WJAssert.notNull(sysRoleOrderReq.getUserId(),"参数错误");
        JSONObject userJson = evsHttpService.getUserById(sysRoleOrderReq.getUserId(), evsHttpService.getToken());
        SysUser sysUser = new SysUser();
        sysUser.setId(userJson.getLong("id"));
        sysUser.setName(userJson.getString("name"));
        sysUser.setTel(userJson.getString("tel"));
        sysRoleOrderService.addDeptRole(sysRoleOrderReq, sysUser,loginSysUser.getName());
        return RespJSON.success();
    }

    /**
     * 根据用户ID查询所拥有工单权限
     *
     * @param id
     * @return RespJSON
     */
    @GetMapping("/getDeptIdListByUserId/{id}")
    public RespJSON getDeptIdListByUserId(@PathVariable Long id,@SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        SysRoleOrder sysRoleOrder = new SysRoleOrder();
        sysRoleOrder.setUserId(id);
        List<SysRoleOrder> list = sysRoleOrderService.getList(sysRoleOrder);
        List<Long> ids=new ArrayList<>();
        for (SysRoleOrder roleOrder : list) {
            ids.add(roleOrder.getDeptId());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ids",ids);
        if(CollectionUtils.isEmpty(ids)){
            jsonObject.put("roleType", EnumOrderRoleType.MY_DEPT_ROLE.getCode());
        } else {
            if(ids.size()==1){
                if(ids.get(0)==1){
                    jsonObject.put("roleType", EnumOrderRoleType.MY_DEPT_AND_CHILDREN_ROLE.getCode());
                }
                if(ids.get(0)==0){
                    jsonObject.put("roleType", EnumOrderRoleType.MY_DEPT_ROLE.getCode());
                }
                if(ids.get(0)==3){
                    jsonObject.put("roleType", EnumOrderRoleType.ALL_ROLE.getCode());
                }
            }else {
                jsonObject.put("roleType", EnumOrderRoleType.CUSTOMIZE_ROLE.getCode());
            }
        }
        return RespJSON.success(jsonObject);
    }

}
